/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.surveilans.controller;

import com.surveilans.classes.InfoSurveilans;
import com.surveilans.classes.Kabupaten;
import com.surveilans.classes.Penyakit;
import com.surveilans.classes.Provinsi;
import com.surveilans.model.DataModel;
import com.surveilans.model.InfoModel;
import com.surveilans.model.UnitModel;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yudi Retanto
 */
@WebServlet(name = "Info", urlPatterns = {"/Info"})
public class InfoController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        UnitModel unit = new UnitModel();
        DataModel data = new DataModel();
        InfoModel info = new InfoModel();
        Calendar cal = Calendar.getInstance();
        request.setAttribute("title", "Info Surveilans");
        if (request.getParameter("type")==null) {
            response.sendRedirect("./info?type=tabel");
        }else if (request.getParameter("type").equals("tabel")) {
            if (request.getMethod().equals("POST")) {
                int prov = Integer.valueOf(request.getParameter("prov"));
                int kabupaten = Integer.valueOf(request.getParameter("kab"));
                int tahun = Integer.valueOf(request.getParameter("tahun"));
                int bulan = Integer.valueOf(request.getParameter("bulan"));
                int penyakit = Integer.valueOf(request.getParameter("penyakit"));
                if(prov<0 || kabupaten<0){
                    //nothing
                } else {
                    ArrayList<InfoSurveilans> ai = new ArrayList<InfoSurveilans>();
                    if (penyakit == 0) {
                        ArrayList<Penyakit> apt = data.getPenyakit();
                        Iterator itr = apt.iterator();
                        while (itr.hasNext()) {
                            Penyakit p = (Penyakit) itr.next();
                            if (kabupaten == 0) {
                                ai.add(info.getInfoProvinsi(p.getId(), prov, tahun, bulan));
                            } else {
                                ai.add(info.getInfo(p.getId(), kabupaten, tahun, bulan));
                            }
                        }
                    } else {
                        if (kabupaten == 0) {
                            ai.add(info.getInfoProvinsi(penyakit, prov, tahun, bulan));
                        } else {
                            ai.add(info.getInfo(penyakit, kabupaten, tahun, bulan));
                        }
                    }
                    cal.set(tahun, bulan - 1, 1);
                    request.setAttribute("dayCount", cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                    if (kabupaten == 0) {
                        request.setAttribute("daerah", info.getNamaProvinsi(prov));
                    } else {
                        request.setAttribute("daerah", info.getNamaKabupaten(kabupaten));
                    }
                    request.setAttribute("info", ai);
                }
            }
            ArrayList<Provinsi> ap = unit.getProvinsi();
            ArrayList<Kabupaten> ak = unit.getKabupaten();
            ArrayList<Penyakit> alp = data.getPenyakit();

            request.setAttribute("provinsi", ap);
            request.setAttribute("kabupaten", ak);
            request.setAttribute("penyakit", alp);
            request.getRequestDispatcher("info/tabel.jsp").forward(request, response);
        }else if (request.getParameter("type").equals("map")) {
            ArrayList<Provinsi> ap = unit.getProvinsi();
            ArrayList<Kabupaten> ak = unit.getKabupaten();
            ArrayList<Penyakit> alp = data.getPenyakit();
            String path = getServletContext().getRealPath("indonesia/data.csv");
            info.createDataCSV(path);

            request.setAttribute("provinsi", ap);
            request.setAttribute("kabupaten", ak);
            request.setAttribute("penyakit", alp);
            request.getRequestDispatcher("info/map.jsp").forward(request, response);
        }else if(request.getParameter("type").equals("ajax")){
            if(request.getParameter("data")==null){
                //null
            }
            else if(request.getParameter("data").equals("kabupaten")){
                int id_prov = Integer.valueOf(request.getParameter("id_prov"));
                ArrayList<Kabupaten> ak = unit.getKabupaten(id_prov);
                response.setContentType("text/plain");
                PrintWriter out = response.getWriter();
                Iterator itr = ak.iterator();
                out.println("<option value=\"0\"> -- Semua Kabupaten -- &nbsp;</option>");
                while (itr.hasNext()) {
                    Kabupaten k = (Kabupaten) itr.next();
                    out.println("<option value=\"" + k.getId() + "\">" + k.getNama() + "</option>");
                }                
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
